package com.ytjj.qmyx.admin.service.impl;

import com.gexin.fastjson.JSON;
import com.gexin.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.constants.SysConfigConstants;
import com.ytjj.qmyx.admin.dao.BankerDao;
import com.ytjj.qmyx.admin.mapper.AdminMapper;
import com.ytjj.qmyx.admin.mapper.AdminRoleRelationMapper;
import com.ytjj.qmyx.admin.mapper.MenuMapper;
import com.ytjj.qmyx.admin.model.*;
import com.ytjj.qmyx.admin.model.request.BankerCheckStatusRequest;
import com.ytjj.qmyx.admin.model.request.BankerRequest;
import com.ytjj.qmyx.admin.model.request.BankerUpdateReleaseRequest;
import com.ytjj.qmyx.admin.model.request.BrandUpdateNameRequest;
import com.ytjj.qmyx.admin.service.BankerService;
import com.ytjj.qmyx.mall.mapper.BankerMapper;
import com.ytjj.qmyx.mall.model.Banker;
import com.ytjj.qmyx.mall.model.BankerExample;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BankerServiceImpl implements BankerService {

    @Resource
    private BankerDao bankerDao;

    @Resource
    private BankerMapper bankerMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private AdminRoleRelationMapper adminRoleRelationMapper;

    @Resource
    private SupplyApiConfig supplyApiConfig;

    @Value("${shiroConfigure.salt}")
    private String salt;

    @Value("${shiroConfigure.hashIterations}")
    private int hashIterations;

    @Override
    public List<Role> getRoleByTel(String tel) {
        return bankerDao.getRoleByTel(tel);
    }

    @Override
    public List<Menu> getBankerMenuList() {
        MenuExample menuExample = new MenuExample();
        menuExample.createCriteria().andIsBankerEqualTo((byte)1);
        List<Menu> menus = menuMapper.selectByExample(new MenuExample());
        return menus;
    }

//    @Override
//    public List<Permission> getPermissionByRoleId(Integer roleId) {
//        return bankerDao.getPermissionByRoleId(roleId);
//    }

    @Override
    public Banker getBankerByTel(String tel) {
        BankerExample bankerExample = new BankerExample();
        bankerExample.createCriteria().andTelEqualTo(tel).andStatusEqualTo(SysConfigConstants.STATUS_1);
        List<Banker> bankers = bankerMapper.selectByExample(bankerExample);
        return bankers.get(0);
    }

//    @Override
//    public List<Menu> getMenuAllByTel(String tel) {
//        return bankerDao.getMenuAllByTel(tel);
//    }

    @Override
    public CommonPage<Admin> getChildren(BankerRequest bankerRequest) {
        PageHelper.startPage(bankerRequest.getPageNum(), bankerRequest.getPageSize());
        List<Admin> childrens = bankerDao.getChildrenById(bankerRequest);
        return CommonPage.restPage(childrens);
    }

    @Override
    public Admin create(Admin admin) {
        // 查看供应商子用户是否存在
        if(adminMapper.checkBankerChildren(admin.getTel(),admin.getBankerId())>0){
            return null;
        }
        //哈希+加盐+次数
        Md5Hash md5SaltIterateHash = new Md5Hash(admin.getPwd(), salt, hashIterations);
        admin.setPwd(md5SaltIterateHash.toString());
        admin.setCreateTime(new Date());
        admin.setStatus(SysConfigConstants.STATUS_1);
        adminMapper.insert(admin);
        return admin;
    }

    @Override
    public int update(Admin admin) {
        admin.setUpdateTime(new Date());
        return adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public int delete(Integer id) {
        return adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Admin getBanker(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Banker> selectBankers() {
        return bankerMapper.selectByExample(new BankerExample());
    }

    @Override
    public int insertNote(YxOrderExceptionNote yxOrderExceptionNote) {
        yxOrderExceptionNote.setCreateTime(new Date());
        return adminMapper.insertNote(yxOrderExceptionNote);
    }

    @Override
    public String newCheckBankerSite(BankerUpdateReleaseRequest request) {
        String json = JSON.toJSONString(request);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/release_site", json, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public String newReleaseInfo(BankerUpdateReleaseRequest request) {
        String json = JSON.toJSONString(request);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/releaseBanker", json, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public String newUpdateBrandName(BrandUpdateNameRequest request) {
        String json = JSON.toJSONString(request);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/updateBrandName", json, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public String newReleaseBankerBrand(BankerUpdateReleaseRequest request) {
        String json = JSON.toJSONString(request);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/release_brand", json, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public String checkBankerSite(BankerCheckStatusRequest request) {
        String json = JSON.toJSONString(request);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/checkBankerSite", json, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public String newCheckBankerBrand(BankerCheckStatusRequest request) {
        String json = JSON.toJSONString(request);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/checkBankerBrand", json, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public CommonResult resetPwd(BrandUpdateNameRequest request) {
        String json = JSON.toJSONString(request);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/resetPwd", json, supplyApiConfig.getHeadsMap());
        CommonResult commonResult = JSONObject.parseObject(resultJson, CommonResult.class);
        return commonResult;
    }

}
